

import {
    DeviceEventEmitter,
    NativeAppEventEmitter,
    NativeEventEmitter,
    NativeModules,
    Platform,
} from 'react-native';

const { RNMpaas } = NativeModules;
function parseAppId(uri) {
  // 从 uri 中解析出来 appId
  let dUri = decodeURIComponent(uri);
  let r = /scheme=mpaas:\/\/platformapi\/startapp\?appId=([^&]*)&/g
  let match = dUri.match(r);
  let appID = RegExp.$1;
  return appID;
}

export default {
    RNMpaas,
    /**
     * @param {String} appID 要启动的小程序的id
     * @param {object} params 传给 mpaas 启动器的参数
     * 示例：{
     *          "a": "field1", // 自定义字段
     *          "b": "field2", // 自定义字段
     *          "c": "field3", // 自定义字段
     *          "notQuery": { // 特殊约定字段 notQuery 字段里对象会解构出来原封不动的传下去，其余字段会被统一包裹在 "query" 字段下，以 key=value 形式：{"query": "key1=value1&key2=value2"}
     *              "page": "pages/xxx/detail", // 指定启动页面，默认 pages/index/index
     *          },
     *      }
     * @param {boolean} hideTitle 是否隐藏原生导航栏，默认 false
     * @param {func} onExit 回调函数，在小程序退出时回调
     * */
    LaunchMini(appID : String, params : object, hideTitle : boolean, onExit : func) {
        let notQuery = params.notQuery || {}
        delete params.notQuery
        params = {
            query: {...params, miniAppId: appID},
            ...notQuery
        }
        if(typeof params.page === 'string') {
            params.page = params.page.trim()
        }
        hideTitle = !!hideTitle
        if(typeof onExit !== 'function') {
            onExit = () => {}
        }
        console.log('启动小程序appid:', appID, "--params:", params, "--hideTitle", hideTitle);
        RNMpaas.LaunchMini(appID, params, hideTitle, onExit);
    },
    parseAppId,
    ScanQRLaunchMini(uri : String, params : object, onExit : func) {
        let notQuery = params.notQuery || {}
        delete params.notQuery
        let appID = parseAppId(uri);
        params = {
            query: {...params, miniAppId: appID || uri},
            ...notQuery
        }
        if(typeof params.page === 'string') {
            params.page = params.page.trim()
        }
        if(typeof onExit !== 'function') {
            onExit = () => {}
        }
        console.log('扫码启动参数：', uri, "--params:", params);
        RNMpaas.ScanQRLaunchMini(uri, params, onExit);
    },
    callbackTiny(callbackid : String, result : object) {
        console.log('回调小程序参数：', callbackid, "--params:", result);
        RNMpaas.callbackTiny(callbackid, result);
    },
    callbackTinyChannel(callbackid : String, result : object) {
        console.log('通过渠道向小程序发消息参数：', callbackid, "--params:", result);
        RNMpaas.callbackTinyChannel(callbackid, result);
    },
    showLoading(text : String) {
        RNMpaas.showLoading && RNMpaas.showLoading(text);
    },
    hideLoading() {
        RNMpaas.hideLoading && RNMpaas.hideLoading();
    },
    bringRnToBack(options : object) {
        console.log('置底 rn 界面 --params:', options);
        RNMpaas.bringRnToBack && RNMpaas.bringRnToBack(options);
    },
    bringRnToFront(options : object) {
        console.log('置顶 rn 界面 --params:', options);
        RNMpaas.bringRnToFront && RNMpaas.bringRnToFront(options);
    },
    registerID(userName : String) {
        console.log('埋点日志上报时的userId：', userName);
        RNMpaas.registerID(userName);
    },
    loginOUt() {
        console.log('当用户退出登录时，清空 userId，以确保相关数据的准确性。');
        RNMpaas.loginOUt();
    },
    updateAllApp() {
        console.log('升级更新小程序到最新版本');
        RNMpaas.updateAllApp();
    },
    startWebView(title : String, url : String, userAgent : String, options : Object) {
        console.log('启动自定义webview');
        RNMpaas.startWebView(title,url,userAgent,options);
    },
};
